package com.m3839.sdk.pay.platform.alipay;

import android.app.Activity;
import android.text.TextUtils;
import android.util.Log;

import com.alipay.sdk.app.PayTask;
import com.m3839.sdk.common.util.ThreadPoolUtil;
import com.m3839.sdk.pay.bean.OrderInfo;
import com.m3839.sdk.pay.platform.PayResultCallBack;
import com.m3839.sdk.pay.platform.PayWrapper;

import java.util.Map;

/**
 * 描述信息:支付宝支付包装类
 *
 * @author chenbaoyang
 * @create 2021/11/3
 */
public class AlipayWrapper implements PayWrapper {

  /**
   * 9000, 支付成功
   * 8000, 支付结果确认中
   * 4000, 系统异常
   * 4001, 数据格式不正确
   * 6001, 取消支付操作
   */

  /** 支付成功 */
  private static final String HYKB_ALIPAY_CODE_SUCCESS = "9000";
  /** 支付结果确认中 */
  private static final String HYKB_ALIPAY_CODE_RESULT_CONFIRM = "8000";
  /** 系统异常 */
  private static final String HYKB_ALIPAY_CODE_SYSTEM_EXCEPTION = "4000";
  /** 数据格式不正确 */
  private static final String HYKB_ALIPAY_CODE_DATA_FORMAT_ERROR = "4001";
  /** 取消支付 */
  private static final String HYKB_ALIPAY_CODE_CANCEL_PAY = "6001";


  private static final String HYKB_ALIPAY_MSG_SUCCESS = "支付成功";
  private static final String HYKB_ALIPAY_MSG_RESULT_CONFIRM = "支付结果确认中";
  private static final String HYKB_ALIPAY_MSG_SYSTEM_EXCEPTION = "系统异常";
  private static final String HYKB_ALIPAY_MSG_DATA_FORMAT_ERROR = "数据格式不正确";
  private static final String HYKB_ALIPAY_MSG_CANCEL_PAY = "取消支付操作";

  @Override
  public void doPay(final Activity activity, final OrderInfo orderInfo,
      final PayResultCallBack callBack) {
    final Runnable payRunnable = new Runnable() {

      @Override
      public void run() {
        if(activity == null) {
          return;
        }
        PayTask alipay = new PayTask(activity);

        String orderStr = orderInfo.payParams.params;
        Map<String, String> result = alipay.payV2(orderStr, true);
        Log.i("msp", result.toString());
        AliPayResult payResult = new AliPayResult(result);
        // 同步返回需要验证的信息
        String resultInfo = payResult.getResult();
        String resultStatus = payResult.getResultStatus();
        // 判断resultStatus 为9000则代表支付成功
        boolean success = TextUtils.equals(resultStatus, HYKB_ALIPAY_CODE_SUCCESS);
        String message = "";
        switch (resultStatus) {
          case HYKB_ALIPAY_CODE_SUCCESS:
            message = HYKB_ALIPAY_MSG_SUCCESS;
            break;
          case HYKB_ALIPAY_CODE_RESULT_CONFIRM:
            message = HYKB_ALIPAY_MSG_RESULT_CONFIRM;
            break;
          case HYKB_ALIPAY_CODE_SYSTEM_EXCEPTION:
            message = HYKB_ALIPAY_MSG_SYSTEM_EXCEPTION;
            break;
          case HYKB_ALIPAY_CODE_DATA_FORMAT_ERROR:
            message = HYKB_ALIPAY_MSG_DATA_FORMAT_ERROR;
            break;
          case HYKB_ALIPAY_CODE_CANCEL_PAY:
            message = HYKB_ALIPAY_MSG_CANCEL_PAY;
            break;
          default:
            break;
        }
        callBack.onResult(success, resultStatus, message);
      }
    };

    ThreadPoolUtil.execute(payRunnable);
  }

}
